<?php 

 /*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Include the header file:
require ('./includes/sidebar.php');
require('Pagination.php');
?>
  
                                      
<?php
  
if (isset($_GET['cid'])) 
{          
        // Typecast it to an integer: An invalid $_GET['cid'] value would be type-casted to 0.              
        $cid = (int) $_GET['cid'];
	    // $cid must have a valid value.
        if ($cid > 0) 
        {  
            
            if(isset($_GET['limit']))
            {
                if($_GET['limit'] != "")
                {
                    $limit = $_GET['limit'];
                }
                else
                {
                    $limit = 5;
                }
            }
            else
            {
                $limit = 5;
            }
            $getRequest = new item();
           
           $getRequest->limit = $limit;
                
           
            if(isset($_GET['page']) && $_GET['page'] != "")
            {
                $page = $_GET['page'];
                $getRequest->page = $page;
            }
            else
            {
                $page = 1;
                $getRequest->page = $page;   
            }
           
            
            $getRequest->category_id=$_GET['cid'];
            
            $category = $_GET['category'];       
              
            //everything in this if statement is to create the layout for the navigation button.
             if ($_GET['parent_category_id']!="")   
             {   
               
                $parent_category_id = $_GET['parent_category_id'];      
                $parent_category = $_GET['parent_category_name']; 
                
                
                $itemcategory = new itemcategory();
                $itemcategory->category_id = $parent_category_id;
                $icRequest = $itemcategory->getcategorybycategoryid();
                
               
                $pcXml = new SimpleXMLElement($icRequest);
                $pcItem_Count = $pcXml->Item_Count;
                
                if($pcItem_Count > 0)
                {
                    foreach($pcXml->Item as $pcItem)
                    {
                        $parentcategory_parentcategory_id = $pcItem->parent_category_id;
                        $parentcategory_parentcategory = "";
                        $itemcategory->category_id = $parentcategory_parentcategory_id;
                        $pcpcRequest = $itemcategory->getcategorybycategoryid();
                        $pcpcXml = new SimpleXMLElement($pcpcRequest);
                        foreach($pcpcXml->Item as $pcpcItem)
                        {
                            $parentcategory_parentcategory = $pcpcItem->category;
                        }
                    }
                

                    echo '<div class="menu2">';  
                    echo ' <b><a href="category.php?&category=' . $parent_category . '&cid=' . $parent_category_id . '&parent_category_id=' . $parentcategory_parentcategory_id . '&parent_category_name=' . $parentcategory_parentcategory . '&limit=' . $limit . '">' . $parent_category . '</a></b>';                          
                    echo '</div>';                                                          
                }
                    
            }
                
                
        }
     
            
            
            //find out if this category has subcategories in it, if so then get them and display them
          
            $getrequest = new itemcategory();
            $cid = (int) $_GET['cid'];                    
            $getrequest->category_id = $cid;
            $getresponse = $getrequest->getchildcategories();  
            
            
            $xml = new SimpleXMLElement($getresponse);
	         echo '<div class="subcategoryheader"> ' . $_GET['category'] . " Category" . '</div>';   
            //get subcategory if there are any
            $Item_Count = $xml->Item_Count;   
            $numsubcategories = $Item_Count;    
            if($xml->Item_Count !=0)
            {   
               
               
               
                   
              foreach($xml->Item as $ItemXml)
              {
                $child_category_id = $ItemXml->category_id;
                $child_category = $ItemXml->category;
                $description = $ItemXml->description;
                $parent_category_id = $ItemXml->parent_category_id;
               
                
                 ?>
                 
                 <div class="menu3">
                   
                 <?php
                 echo ' <b><a href="category.php?cid=' . $child_category_id . '&category=' . $child_category . '&parent_category_id=' . $parent_category_id . '&parent_category_name=' . $category . '&limit=' . $limit . '">' . $child_category . '</a></b>';   
                 echo '</div>';   
                
              }
              echo '<br /><br /><br />';    
               
            } 
  ////////////////////////////////////////////////////////////////////////////////////////////////E N D   C A T E G O R Y  LINK  BUILDING  SECTION////////////////////////////////////////////////////////       
      
 
 /////////////////////////////////////////////////////////////////////////////////////////////////S T A R T   I T E M   R E T R I E V A L/////////////////////////////////////////////////////////////////
    // Get the information from the database
    // for this category:
    $Pagination = new Pagination();

    if(isset($_GET['totalitems']))
    {
        if($_GET['totalitems'] != "")
        {
            $totalitems = $_GET['totalitems'];
        }      
    }
    
     
 
    $getResponse = $getRequest->getitembycategory();     
    $xml = new SimpleXMLElement($getResponse);
  
    
   
    $Item_Count = $xml->Item_Count;
    $Item_Count = (int)$Item_Count;
    $Pagination->page = $page;
    $Pagination->limit = $limit;
   
    $Pagination->setTotalItems($Item_Count);
 
    
   
    if($Item_Count == 0)
    {
         echo '<br /><br /><div class="noitemsheader">No items in this category</div>';    
    }
   
  
    $parent_category = "";
    if(isset($_GET['parent_category']))
    {
        $parent_category = $_GET['parent_category'];
    }
    
    
    ?>
    <script>
   
    function changeImage(sw_id)
    {
       
        var idImageArray = new Array();
        var swIdgwIdArray = new Array();
    
    <?php
    
    foreach ($xml->Item as $ItemXml)
    {
        
        $specialized = (string) $ItemXml->specialized;
     
        if($specialized == "true")
        {
            $specialization = $ItemXml->specialization;
            
            $gw_id = $ItemXml->gw_id;
            $first_image = "";
           
            foreach($specialization->Item as $eachitem_spec)
            {
                $this_qty = $eachitem_spec->qty;
                $this_price = $eachitem_spec->price;
                $this_weight = $eachitem_spec->weight;
                
                if($this_qty > 0 && $this_price > 0 && $this_weight > 0)
                {
                    
                    
                    $this_image = (string) urldecode($eachitem_spec->image);
                    $this_image = checkimage($this_image);
                    $this_sw_id = (string) $eachitem_spec->sw_id;
                    echo 'idImageArray["' . $this_sw_id . '"] = "' . $this_image . '";';
                    echo 'swIdgwIdArray["' . $this_sw_id . '"] = "' . $gw_id . '";';
                    

                }
                    
            }
                
        }
        
    }
    ?>
        var gw_id = swIdgwIdArray[sw_id];
        var imageHtml = document.getElementById(gw_id);
        var imageLocation = idImageArray[sw_id];
        imageHtml.src = imageLocation;
    
    }
    </script>
    <br /><br /><br />
    <?php
     
    foreach ($xml->Item as $ItemXml)
    {   
        
        //Need to check to see if item is specialized.
        $specialized = $ItemXml->specialized;
        $specialized = (string)$specialized;
       
        $gw_id = $ItemXml->gw_id;
        $gw_id = (int)$gw_id;
        $gw_name = $ItemXml->gw_name;
        $category_id = $ItemXml->category_id;
        $category = $ItemXml->category;
        $general_description = $ItemXml->general_description;

        
        $image = (string) $ItemXml->image;
        $image = urldecode($image);
        if($specialized == 'false')
        {
            $first_image = $image;   
        }
        
        $price = $ItemXml->price;
        $in_stock = $ItemXml->in_stock;
        if($specialized == 'false')
        {
            
            
            echo imageresize(.25, $image);
            ?>
            
            <table cellpadding="2" cellspacing="3" width="100%" border="0" class="content_box">
             <form action="category.php" method="GET" id="category_info">        
            <tr>
                <td align="left"><b>Name:</b><a href="product.php?category=<?php echo$_GET['category'];?>&page_title=Product Page&page=<?php echo $page;?>&gw_id=<?php echo $gw_id?>&gw_name=<?php echo $gw_name?>&cid=<?php echo $cid ?>&parent_category_id=<?php echo $_GET['parent_category_id'];?>&parent_category_name=<?php echo $_GET['parent_category_name'];?>"><?php echo $gw_name?></a></li></td>
            </tr>
                     
            <tr>
                <td align="left"><b>General Description:</b><?php echo $general_description; ?></a></li></td>
            </tr>
            <tr>
                <td align="left"><b>Price: </b>$<?php echo $price; ?></li></td>     
            </tr>
                 
            <tr>
            <?php
                if ($in_stock > 0)
                {    
                     echo '</tr>';
                    echo '<tr>';
                    echo '<input type="hidden" id="category" name="category" value="' . $_GET['category'] . '">';
                    echo '<input type="hidden" id="page" name="page" value="' . $_GET['page'] . '">';          
                    echo '<input type="hidden" id="parent_category_id" name="parent_category_id" value="' . $_GET['parent_category_id'] . '">';
                    echo '<input type="hidden" id="parent_category_name" name="parent_category_name" value="' . $parent_category . '">';
                    echo '<input type="hidden" id="dp" name="do" value="add">'; 
                    echo '<input type="hidden" id="cid" name="cid" value="' . $category_id . '">'; 
                    echo '<input type="hidden" id="gw_id" name="gw_id" value="' . $gw_id . '">';
                    echo '<input type="hidden" id="sw_id" name="sw_id" value="">';  
                    echo '<td>';     
                    echo '<input type="submit" id="submit" name="submit" value="Add To Cart">';
                    echo '</td>';
                    echo '</tr>';
                
                    echo '</form>';
                    echo '</table>';
                    echo '<br />';
                           
                }
                else
                {
                    echo   '<tr>
                            <td align="left"><b>Out Of Stock</b></td>
                            </tr>
                            ';
                }       
                ?> 
                 </tr>
                 <br />
                 
                 </table> 
                 <?php            
      
        }
        else //specialized == true
        {

           
            $specialization = $ItemXml->specialization;
            $first_image = "";
            $firstimagecounter = 0;
            foreach($specialization->Item as $eachitem_spec)
            {
                $this_sw_id = $eachitem_spec->sw_id;  
                $this_image = (string)$eachitem_spec->image;  
                $this_image = (string) $this_image;   
                if($firstimagecounter == 0)
                {
                    $first_image = $this_image;
                }
                $firstimagecounter++;
            }
            if($first_image == "")
            {
                $first_image = $image;
            }
            
            echo imageresizedimensions($gw_id, .25, $first_image);
                      ?>
                           
                    <br /><b>Name:</b><a href="product.php?category=<?php echo$_GET['category'];?>&page_title=Product Page&page=<?php echo $page; ?>&gw_id=<?php echo $gw_id?>&gw_name=<?php echo $gw_name?>&cid=<?php echo $cid ?>&parent_category_id=<?php echo $_GET['parent_category_id'];?>&parent_category_name=<?php echo $_GET['parent_category_name'];?>"><?php echo $gw_name?></a><br />
                    <b>General Description:</b><?php echo $general_description;?><br />       
                    <form action="category.php" method="GET" id="general">
                    <input type="hidden" id="gw_id" name="gw_id" value="<?php echo $gw_id;?>">
                    <input type="hidden" id="parent_category_name" name="parent_category_name" value="<?php echo $parent_category;?>">       
                    <input type="hidden" id="parent_category_id" name="parent_category_id" value="<?php echo $_GET['parent_category_id'];?>">
                    <input type="hidden" id="category" name="category" value="<?php echo $_GET['category']?>">  
                    <input type="hidden" id="do" name="do" value="add">
            <?php
            
          
            ?>
            <td><b>Choose type of item:</b></td>
            <select id="sw_id" name="sw_id" onchange="changeImage(this.value);">             
            
            <?php
            $swinfoCount = 0;

            foreach($specialization->Item as $eachitem_spec)
            {
                
                $this_sw_id = $eachitem_spec->sw_id;
                $this_price = (double)$eachitem_spec->price; 
                $this_weight = (double)$eachitem_spec->weight;
                $this_image = (string)$eachitem_spec->image;
                $this_image = urldecode($this_image);
                $this_qty = $eachitem_spec->qty;
                $this_qty = (int)$this_qty; 

                if($this_qty > 0 && $this_price > 0 && $this_weight > 0)
                {
                   
                    $this_image = $eachitem_spec->image;
                    $this_description = $eachitem_spec->description;
                    $descrip_build = "";
                    foreach($this_description->Item as $eachitem_desc)
                    {
                        $this_isd_id = $eachitem_desc->isd_id;
                        $this_descriptor = $eachitem_desc->descriptor;
                        $this_category = $eachitem_desc->category;
                        $descrip_build .= $this_category . '=' . $this_descriptor . ", ";
                      
                    } 
                    $descrip_build .= 'Price: $' . $this_price;
                   
                    echo '<option value="' . $this_sw_id . '">' . $descrip_build . '</option>'; 
                    $swinfoCount++;        
                }
                 
            }   
            if($swinfoCount == 0)
            {
                echo '<option value="0">Out Of Stock</option>';  
            }
?>  
            
              
                  </select>
                  <br />
                  <input type="hidden" id="cid" name="cid" value="<?php echo $_GET['cid']?>">   
                  <input type="hidden" id="page" name="page" value="<?php echo $page; ?>"> 
<?php
            if($swinfoCount != 0)
            {
                echo '<input type="submit" id="submit" value="Add To Cart">'; 
            }
?>
                
                
                  
            </form>               
            <br />
            <br />
            <br />
<?php    

        }
        
      
       
 }
    
////////////////////////////S T A R T  P A G I N A T I O N  C O D E ////////////////////////////////S T A R T  P A G I N A T I O N  C O D E ///////////////////////////////////////////////////////////////////////  
    $adjacents = 3;
    $targetpage = "category.php";   
    $parent_category_id = $_GET['parent_category_id'];  
    $pciNameValue = "parent_category_id=$parent_category_id";
    
    $category = $_GET['category'];
    $categoryNameValue = "category=$category"; 
    
    $cidNameValue = "cid=$category_id";
    
    $pcnNameValue= "parent_category_name=$parent_category";
    
    $Pagination->setTargetPage($targetpage);
    $Pagination->addToUrlVarArray($pciNameValue);
    $Pagination->addToUrlVarArray($categoryNameValue);
    $Pagination->addToUrlVarArray($cidNameValue);
    $Pagination->addToUrlVarArray($pcnNameValue);
    
    $Pagination->setUrlVariables();
    if($Item_Count > 0)
    {
      $Pagination->getPaginationLinkBar();   
    }
    
/////////////////////////////////////////////////////////////////////E N D  P A G I N A T I O N  C O D E//////////////////////////////////////////////////////////////////////////////////////////////
}                 
else 
{ // they didnt enter the page correctly
 
		echo '<p class="error">You have reached this page in error. Please choose a category from the panel on your right hand side.</p>'; 
       
       
}

// Include the footer file to complete the template:

include_once ('./includes/footer.html');
echo '<br />';
?>  
